package com.example.suze.libraryinformationmanagementsystem.Fragment;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.suze.libraryinformationmanagementsystem.Activity.AddActivity;
import com.example.suze.libraryinformationmanagementsystem.Activity.MainActivity;
import com.example.suze.libraryinformationmanagementsystem.Activity.ResultActivity;
import com.example.suze.libraryinformationmanagementsystem.Activity.Search;
import com.example.suze.libraryinformationmanagementsystem.R;
import com.person.CommonUtils;
import com.person.entity.Book;
import com.person.entity.Person;
import com.person.entity.Student;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * Created by suze on 2017/6/10.
 */

public class FindFragment extends Fragment implements View.OnClickListener{
    private EditText content;
    private Button search;
    private String txt;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        super.onCreateView(inflater, container, savedInstanceState);
        View chatView = inflater.inflate(R.layout.fragment_find, container,false);
        return chatView;


    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        content = (EditText) getView().findViewById(R.id.content);
        search = (Button) getView().findViewById(R.id.search);
        search.setOnClickListener(this);
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.search:
                txt = content.getText().toString();
                if (txt.equals("")) {
                    MainActivity mainActivity = (MainActivity) getActivity();
                    Toast.makeText(mainActivity,"请输入查找内容",Toast.LENGTH_SHORT).show();
                } else {
                    if (txt.equals("12138")) {
                        insertData();
                    } else {
                        MainActivity mainActivity = (MainActivity) getActivity();
                        Bundle bundle = new Bundle();
                        Intent intent = new Intent(mainActivity, ResultActivity.class);
                        bundle.putString("search", txt);
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                    break;
                }
        }
    }
    /**
     * bitmap转为base64
     * @param bitmap
     * @return
     */
    public static String bitmapToBase64(Bitmap bitmap) {

        String result = null;
        ByteArrayOutputStream baos = null;
        try {
            if (bitmap != null) {
                baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

                baos.flush();
                baos.close();

                byte[] bitmapBytes = baos.toByteArray();
                result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
            }
        } catch (IOException e){
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.flush();
                    baos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
    private void insertData(){
        MainActivity mainActivity = (MainActivity) getActivity();
        CommonUtils commonUtils = new CommonUtils(mainActivity);
        Book book = new Book();
        Resources res = getResources();



        Bitmap bmp10 = BitmapFactory.decodeResource(res, R.drawable.wuyanzu);
        String img10 = bitmapToBase64(bmp10);
        Student student = new Student();
        student.setName("吴彦祖");
        student.setClass1("信安1401");
        student.setSchool("计算机科学与技术学院");
        student.setProfession("信息安全");
        student.setId(5120145303l);
        student.setHead(img10);
        commonUtils.insertStudent(student);

        Bitmap bmp11 = BitmapFactory.decodeResource(res, R.drawable.jinchengwu);
        String img11 = bitmapToBase64(bmp11);
        student.setName("赖大铖");
        student.setClass1("信安1405");
        student.setSchool("计算机科学与技术学院");
        student.setProfession("信息安全");
        student.setId(5120147777l);
        student.setHead(img11);
        commonUtils.insertStudent(student);


        Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.busibumie);
        String img = bitmapToBase64(bmp);
        book.setName("不死不灭");
        book.setAuthor("辰东");
        book.setPublishing("起点中文网");
        book.setBorrowerId(1l);
        book.setTimeOut("");
        book.setTimeIn("");
        book.setTag(1);
        book.setStatus(0);
        book.setId(1000l);
        book.setImg(img);
        book.setAbstract1("“历千劫万险，纵使魂飞魄散，我灵识依在，战百世轮回，纵使六道无常，我依然永生！天道！天道！天已失道，何需奉天，道既死，魔应生！”\n" +
                "一个被称为魔的人，为了生存而苦苦挣扎，最后走上了一条抗天之路……");
        commonUtils.insertFriend(book);


        Bitmap bmp1 = BitmapFactory.decodeResource(res, R.drawable.dazhuzai);
        String img1 = bitmapToBase64(bmp1);
        book.setName("大主宰");
        book.setAuthor("辰东");
        book.setPublishing("起点中文网");
        book.setBorrowerId(1l);
        book.setTimeOut("");
        book.setTimeIn("");
        book.setTag(1);
        book.setStatus(0);
        book.setId(1001l);
        book.setImg(img1);
        book.setAbstract1("大千世界，位面交汇，万族林立，群雄荟萃，一位位来自下位面的天之至尊，在这无尽世界，演绎着令人向往的传奇，追求着那主宰之路。\n" +
                "　　无尽火域，炎帝执掌，万火焚苍穹。\n" +
                "　　武境之内，武祖之威，震慑乾坤。\n" +
                "　　西天之殿，百战之皇，战威无可敌。\n" +
                "　　北荒之丘，万墓之地，不死之主镇天地。\n" +
                "　　......\n" +
                "　　少年自北灵境而出，骑九幽冥雀，闯向了那精彩绝伦的纷纭世界，主宰之路，谁主沉浮？\n" +
                "　　大千世界，万道争锋，吾为大主宰");
        commonUtils.insertFriend(book);


        Bitmap bmp2 = BitmapFactory.decodeResource(res, R.drawable.doupo);
        String img2 = bitmapToBase64(bmp2);
        book.setName("斗破苍穹");
        book.setAuthor("天蚕土豆");
        book.setPublishing("起点中文网");
        book.setBorrowerId(1l);
        book.setTimeOut("");
        book.setTimeIn("");
        book.setTag(1);
        book.setStatus(0);
        book.setId(1002l);
        book.setImg(img2);
        book.setAbstract1("这里是属于斗气的世界，没有花俏艳丽的魔法，有的，仅仅是繁衍到巅峰的斗气！\n" +
                "　　新书等级制度：斗者，斗师，大斗师，斗灵，斗王，斗皇，斗宗，斗尊，斗圣，斗帝。");
        commonUtils.insertFriend(book);


        Bitmap bmp3 = BitmapFactory.decodeResource(res, R.drawable.qingyunian);
        String img3 = bitmapToBase64(bmp3);
        book.setName("庆余年");
        book.setAuthor("猫腻");
        book.setPublishing("起点中文网");
        book.setBorrowerId(1l);
        book.setTimeOut("");
        book.setTimeIn("");
        book.setTag(1);
        book.setStatus(0);
        book.setId(1003l);
        book.setImg(img3);
        book.setAbstract1("积善之家，必有余庆，留余庆，留余庆，忽遇恩人；幸娘亲，幸娘亲，积得阴功。劝人生，济困扶穷……而谁可知，人生于世，上承余庆，终究却是要自己做出道路抉择，正是所谓岔枝发：\n" +
                "　　东风携云雨，幼藤吐新芽。\n" +
                "　　急催如颦鼓，洗尽茸与华。\n" +
                "　　且待朝阳至，绿遍庭中架。\n" +
                "　　更盼黄叶时，采得数枚瓜。\n" +
                "　　……\n" +
                "　　……");
        commonUtils.insertFriend(book);


        Bitmap bmp4 = BitmapFactory.decodeResource(res, R.drawable.xiyouji);
        String img4 = bitmapToBase64(bmp4);
        book.setName("西游记");
        book.setAuthor("吴承恩");
        book.setPublishing("中国人民出版社");
        book.setBorrowerId(1l);
        book.setTimeOut("");
        book.setTimeIn("");
        book.setTag(1);
        book.setStatus(0);
        book.setId(1004l);
        book.setImg(img4);
        book.setAbstract1("在遥远的古代，东方傲来国有一座花果山，山上有一块仙石吸收日月精华，内育仙胞，一日迸裂，产一个石猴。这石猴同一群猴子在山中避暑，无意之间发现了一股飞瀑从山顶流下。石猴自告奋勇，钻入飞瀑，发现里面竟然是一个宽敞、幽静的石洞，众猴拜石猴为王，称他美猴王。");
        commonUtils.insertFriend(book);


        Bitmap bmp5 = BitmapFactory.decodeResource(res, R.drawable.xuezhong);
        String img5 = bitmapToBase64(bmp5);
        book.setName("雪中悍刀行");
        book.setAuthor("烽火戏诸侯");
        book.setPublishing("纵横中文网");
        book.setBorrowerId(1l);
        book.setTimeOut("");
        book.setTimeIn("");
        book.setTag(1);
        book.setStatus(0);
        book.setId(1005l);
        book.setImg(img5);
        book.setAbstract1("江湖是一张珠帘。 大人物小人物，是珠子，大故事小故事，是串线。 情义二字，则是那些珠子的精气神。 ———— 开始收官中。 最终章将以那一声“小二上酒”结尾。");
        commonUtils.insertFriend(book);


        Bitmap bmp6 = BitmapFactory.decodeResource(res, R.drawable.zhetian);
        String img6 = bitmapToBase64(bmp6);
        book.setName("遮天");
        book.setAuthor("辰东");
        book.setPublishing("起点中文网");
        book.setBorrowerId(1l);
        book.setTimeOut("");
        book.setTimeIn("");
        book.setTag(1);
        book.setStatus(0);
        book.setId(1006l);
        book.setImg(img6);
        book.setAbstract1("冰冷与黑暗并存的宇宙深处，九具庞大的龙尸拉着一口青铜古棺，亘古长存。 \n" +
                "　　这是太空探测器在枯寂的宇宙中捕捉到的一幅极其震撼的画面。 \n" +
                "　　九龙拉棺，究竟是回到了上古，还是来到了星空的彼岸？ \n" +
                "　　一个浩大的仙侠世界，光怪陆离，神秘无尽。热血似火山沸腾，激情若瀚海汹涌，欲望如深渊无止境…… \n" +
                "　　登天路，踏歌行，弹指遮天。 ");
        commonUtils.insertFriend(book);


        Bitmap bmp7 = BitmapFactory.decodeResource(res, R.drawable.zetianji);
        String img7 = bitmapToBase64(bmp7);
        book.setName("择天记");
        book.setAuthor("猫腻");
        book.setPublishing("腾讯文学");
        book.setBorrowerId(1l);
        book.setTimeOut("");
        book.setTimeIn("");
        book.setTag(1);
        book.setStatus(0);
        book.setId(1007l);
        book.setImg(img7);
        book.setAbstract1("太始元年，有神石自太空飞来，分散落在人间，其中落在东土大陆的神石，上面镌刻着奇怪的图腾，人因观其图腾而悟道，后立国教。 数千年后，十四岁的少年孤儿陈长生，为治病改命离开自己的师父，带着一纸婚约来到神都，从而开启了一个逆天强者的崛起征程。");
        commonUtils.insertFriend(book);


        Bitmap bmp12 = BitmapFactory.decodeResource(res, R.drawable.lubinxun);
        String img12 = bitmapToBase64(bmp12);
        book.setName("Robinson Crusoe");
        book.setAuthor("Daniel Dafoe");
        book.setPublishing("China Friendship Company");
        book.setBorrowerId(1l);
        book.setTimeOut("");
        book.setTimeIn("");
        book.setTag(2);
        book.setStatus(0);
        book.setId(1008l);
        book.setImg(img12);
        book.setAbstract1("Robinson Caruso was born in a respectable merchant family, eager to sail, and wanted to see something overseas. He went out to London without his father, bought some fake pearls and toys from Africa and did business in africa.\n" +
                "The fourth voyage, the ship ran aground in a storm on the way, all the companions were killed on board, only Robinson survived, drifted alone a no human habitation of the island.");
        commonUtils.insertFriend(book);


        Bitmap bmp13 = BitmapFactory.decodeResource(res, R.drawable.nicai);
        String img13 = bitmapToBase64(bmp13);
        book.setName("Notre Dame de Paris");
        book.setAuthor("Victor Hugo");
        book.setPublishing("Jilin publishing");
        book.setBorrowerId(1l);
        book.setTimeOut("");
        book.setTimeIn("");
        book.setTag(2);
        book.setStatus(0);
        book.setId(1009l);
        book.setImg(img13);
        book.setAbstract1("Fifteenth Century in Paris, on April Fool's day. Paris people carrying the deformed \"king of fools\" - the Notre Dame de Paris bell Casey Modoo, in front of the Cathedral Square parade cheer glaive. The gypsy girl Esmeirada with a lamb dance performers, poor poet Gringoire captivated by her beauty and dancing. He heard her lovely singing in the night, with her when suddenly unable to restrain the emotions out of the two men she stole, he recognized one of them is the ugly Casey Modoo, Casey Modoo was knocked out.");
        commonUtils.insertFriend(book);


        Bitmap bmp14 = BitmapFactory.decodeResource(res, R.drawable.yeye);
        String img14 = bitmapToBase64(bmp14);
        book.setName("Mrs Bovary");
        book.setAuthor("Gustave Flaubert");
        book.setPublishing("Jilin publishing");
        book.setBorrowerId(1l);
        book.setTimeOut("");
        book.setTimeIn("");
        book.setTag(2);
        book.setStatus(0);
        book.setId(1010l);
        book.setImg(img14);
        book.setAbstract1("Charlie Bovary is the son of an army surgeon. He is not gifted, but diligent, honest, and weak. Father does not pay much attention to education. At the age of twelve, he was given the right to go to school by his mother and later became a doctor. When his parents found him one thousand and two hundred francs income Widow - Dolby g lady wife a year, she was forty-five years old, old and ugly, \"as wood to dry, like a pimple face like the spring sprout\". But because she is not the lack of money, the husband should be elected.");
        commonUtils.insertFriend(book);



        Bitmap bmp15 = BitmapFactory.decodeResource(res, R.drawable.haha);
        String img15 = bitmapToBase64(bmp15);
        book.setName("Greek mythology");
        book.setAuthor("All-Star");
        book.setPublishing("Jilin publishing");
        book.setBorrowerId(1l);
        book.setTimeOut("");
        book.setTimeIn("");
        book.setTag(2);
        book.setStatus(0);
        book.setId(1011l);
        book.setImg(img15);
        book.setAbstract1("Greek mythology consists mainly of the story of God and the legend of heroes. The story of God consists of the creation of heaven and earth, the birth of God, the genealogy of God, the activities of God and the origin of mankind. In Greek mythology, God divided the old gods and the new gods, the old God of Gaea and Uranus as the representative of the new God is dominated by Zeus Olympus Gods (twelve God etc.).");
        commonUtils.insertFriend(book);


        Bitmap bmp16 = BitmapFactory.decodeResource(res, R.drawable.fushengliuji);
        String img16 = bitmapToBase64(bmp16);
        book.setName("浮生六记");
        book.setAuthor("沈复");
        book.setPublishing("天津人民出版社");
        book.setBorrowerId(1l);
        book.setTimeOut("");
        book.setTimeIn("");
        book.setTag(3);
        book.setStatus(0);
        book.setId(1012l);
        book.setImg(img16);
        book.setAbstract1("《浮生六记》以作者夫妇生活为主线，赢余了平凡而又充满情趣的居家生活的浪游各地的所见所闻。作品描述了作者和妻子陈芸情投意合，想要过一种布衣蔬食而从事艺术的生活，由于封建礼教的压迫与贫困生活的煎熬，终至理想破灭。本书文字清新真率，无雕琢藻饰痕迹，情节则伉俪情深，至死不复；始于欢乐，终于忧患，漂零他乡，悲切动人。此外，本书还收录了清代名士冒襄悼念秦淮名妓董小宛的佳作《影梅庵忆语》。");
        commonUtils.insertFriend(book);



        Bitmap bmp17 = BitmapFactory.decodeResource(res, R.drawable.dushuyulvxing);
        String img17 = bitmapToBase64(bmp17);
        book.setName("读书与旅行");
        book.setAuthor("张志宏");
        book.setPublishing("北京人民出版社");
        book.setBorrowerId(1l);
        book.setTimeOut("");
        book.setTimeIn("");
        book.setTag(3);
        book.setStatus(0);
        book.setId(1013l);
        book.setImg(img17);
        book.setAbstract1("《旅行与读书》是一本没有图片的旅行游记，不提供可以按图索骥的旅行指南，却带有丰富的想象与画面感。永不止步的前行者詹宏志，总是在阅读，始终在路上。他的旅行多由读书而起，十个推理小说般的精彩故事，犹如一场百科知识的脑内冲浪与日常哲思的马拉松。被一本托斯卡纳食谱指南引路的摊牌考验；因尽信书而惊险万分的瑞士登山之旅；在印度无力招架念诵着鲁拜集的高明地毯商人；在南非草丛中充满生命体验的萨伐旅；在灾难过后的巴厘岛矛盾复杂的旅人心境……十场梦幻旅，别样新人生。");
        commonUtils.insertFriend(book);



        Bitmap bmp18 = BitmapFactory.decodeResource(res, R.drawable.baishuo);
        String img18 = bitmapToBase64(bmp18);
        book.setName("白说");
        book.setAuthor("白岩松");
        book.setPublishing("新浪文学");
        book.setBorrowerId(1l);
        book.setTimeOut("");
        book.setTimeIn("");
        book.setTag(3);
        book.setStatus(0);
        book.setId(1014l);
        book.setImg(img18);
        book.setAbstract1("《白说》是央视资深新闻人白岩松继《幸福了吗》《痛并快乐着》之后的全新作品，一部言语中的心灵之路。通过近年来于各个场合与公众的深入交流，以平等自由的态度，分享其世界观和价值观。时间跨度长达十五年，涵盖时政、教育、改革、音乐、阅读、人生等多个领 域，温暖发声，理性执言。\n" +
                "在有权保持沉默的年纪拒绝沉默，为依然热血有梦的人们敲鼓拨弦。尽管“说话不是件好玩的事儿”，依然向往“说出一个更好点儿的未来”，就算“说了白说”，可是“不说，白不说”。");
        commonUtils.insertFriend(book);



        Bitmap bmp19 = BitmapFactory.decodeResource(res, R.drawable.huozhe);
        String img19 = bitmapToBase64(bmp19);
        book.setName("活着");
        book.setAuthor("余华");
        book.setPublishing("上海文艺出版社");
        book.setBorrowerId(1l);
        book.setTimeOut("");
        book.setTimeIn("");
        book.setTag(3);
        book.setStatus(0);
        book.setId(1015l);
        book.setImg(img19);
        book.setAbstract1("《活着》讲述一个人一生的故事，这是一个历尽世间沧桑和磨难老人的人生感言，是一幕演绎人生苦难经历的戏剧。小说的叙述者“我”在年轻时获得了一个游手好闲的职业——去乡间收集民间歌谣。在夏天刚刚来到的季节，遇到那位名叫福贵的老人，听他讲述了自己坎坷的人生经历： 地主少爷福贵嗜赌成性，终于赌光了家业一贫如洗，穷困之中福贵因母亲生病前去求医，没想到半路上被国民党部队抓了壮丁，后被解放军所俘虏，回到家乡他才知道母亲已经过世，妻子家珍含辛茹苦带大了一双儿女，但女儿不幸变成了哑巴。");
        commonUtils.insertFriend(book);



        Bitmap bmp20 = BitmapFactory.decodeResource(res, R.drawable.pinang);
        String img20 = bitmapToBase64(bmp20);
        book.setName("皮囊");
        book.setAuthor("蔡崇达");
        book.setPublishing("上海文艺出版社");
        book.setBorrowerId(1l);
        book.setTimeOut("");
        book.setTimeIn("");
        book.setTag(3);
        book.setStatus(0);
        book.setId(1016l);
        book.setImg(img20);
        book.setAbstract1("一部有着小说阅读质感的散文集，也是一本“认心又认人”的书。\n" +
                "作者蔡崇达，本着对故乡亲人的情感，用一种客观、细致、冷静的方式，讲述了一系列刻在骨肉间故事。一个福建渔业小镇上的风土人情和时代变迁，在这些温情而又残酷的故事中一一体现。用《皮囊》这个具有指向本质意味的书名，来 表达作者对父母、家乡的缅怀，对朋友命运的关切，同时也回答那些我们始终要回答的问题。");
        commonUtils.insertFriend(book);



    }
}
